package com.ali.Reggie.controller;

import com.ali.Reggie.common.CustomException;
import com.ali.Reggie.common.R;
import com.ali.Reggie.entity.Orders;
import com.ali.Reggie.service.OrdersService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author ：ALi
 * @date ：Created in 2022/6/15 10:41
 * @description：订单类实现类
 * @modified By：
 * @version: $
 */
@RestController
@Slf4j
@RequestMapping("/order")
public class OredersController {

    @Autowired
    private OrdersService ordersService;

    //点击"去支付"保存订单到数据库
    @PostMapping("/submit")
    public R<String> submit(@RequestBody Orders orders){
        //封装为了一个方法submit
        ordersService.submit(orders);
        return R.success("下单成功");
    }

    //后端的订单明细
    @GetMapping("/page")
    public R<Page<Orders>> page(int page,int pageSize,Long number,String beginTime,String endTime){
        Page<Orders> page1 = new Page<>(page,pageSize);
        //构建条件构造器
        LambdaQueryWrapper<Orders> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(number != null,Orders::getNumber,number)
                .gt(StringUtils.isNotEmpty(beginTime),Orders::getOrderTime,beginTime)
                .lt(StringUtils.isNotEmpty(endTime),Orders::getOrderTime,endTime);
        Page<Orders> ordersPage = ordersService.page(page1, queryWrapper);
        return R.success(ordersPage);
    }

    //派送
    @PutMapping()
    public R<String> arrive(@RequestBody Orders orders){
        Long id = orders.getId();
        if(id == null){
            throw new CustomException("订单为空，无法派送");
        }
        ordersService.updateById(orders);
        return R.success("派送成功");
    }



}